<!-- src\App.vue -->
<template>
	<h1>ToDo App</h1>
	<form @submit.prevent="addTodo()">
		<label >添加待办项</label>
		<input type="text" name="newTodo" autocomplete="off" v-model.trim="newTodo" />
		<button>添 加</button>
	</form>

	<h2>待办列表</h2>
	<ul>
		<li v-for="(todo, index) in todos" :key="todo.id">
			<span :class="{ done: todo.done }" @click="doneTodo(todo)">{{ todo.content }}</span>
			<button @click="removeTodo(index)">删 除</button>
		</li>
	</ul>
	<h4 v-if="todos.length === 0">待办列表为空</h4>
</template>

<script>
import { ref } from 'vue';
export default {
	name: 'App',
	setup() {
		const newTodo = ref('');
		let index = 0;
		const defaultData = [{
				id: index++,
				done: false,
				content: '今天要学习Vue'
			}];

		const todosData = JSON.parse(localStorage.getItem('todos')) || defaultData;
		const todos = ref(todosData);

		function addTodo() {
			if (newTodo.value) {
				todos.value.push({
					id: index++,
					done: false,
					content: newTodo.value
				})
				newTodo.value = ''
				saveData()
			}

		}
		function doneTodo (todo) {
			todo.done = !todo.done
			saveData();
		}
		function removeTodo(index) {
			todos.value.splice(index, 1)
			saveData()
		}
		function saveData() {
			const storageData = JSON.stringify(todos.value)
			localStorage.setItem('todos', storageData)
		}

		return {
			newTodo,
			todos,
			addTodo,
			removeTodo,
			doneTodo
		}
	}
}
</script>

<style lang="scss">
$size1: 6px;
$size2: 12px;
$size3: 18px;
$size4: 24px;
$size5: 48px;
$backgroundColor: #27292d;
$primaryColor: #EC23F3;
$secondTextColor: #1f2023;

$border: 2px solid rgba($color: white,
		$alpha: 0.35,
	);

$textColor: white;
$border_r: 2px solid red;
$border_y: 2px solid rgb(241, 229, 50);
$border_g: 2px solid rgb(50, 241, 50);
$border_w: 2px solid white;

body {
	margin: 0;
	padding: 0;
	font-family: Avenir, Helvetica, Arial, sans-serif;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
	background-color: $backgroundColor;
	color: $textColor;

	#app {
		max-width: 600px;
		margin-left: auto;
		margin-right: auto;
		padding: 20px;
		// border: $border_w;

		h1 {
			font-weight: bold;
			font-size: 28px;
			text-align: center;
			// border: $border_r;
		}

		form {
			display: flex;
			flex-direction: column;
			width: 100%;

			label {
				font-size: 14px;
				font-weight: bold;
			}

			input,
			button {
				height: $size5;
				box-shadow: none;
				outline: none;
				padding-left: $size2;
				padding-right: $size2;
				border-radius: $size1;
				font-size: 18px;
				margin-top: $size1;
				margin-bottom: $size2;
				transition: all 0.2s ease-in-out;
				/* 添加过渡效果，使变化平滑 */
			}

			input {
				background-color: transparent;
				border: $border;
				color: inherit;
			}

			input:hover {
				border: 2px solid rgb(236, 35, 243);
			}

			button {
				cursor: pointer;
				background-color: rgb(236, 35, 243);
				border: 1px solid $primaryColor;
				font-weight: bold;
				color: white;
				border-radius: $size1;
			}
		}

		h2 {
			// border: $border_g;
			font-size: 22px;
			border-bottom: $border;
			padding-bottom: $size1;

		}

		ul {
			padding: 10px;

			li {
				display: flex;
				justify-content: space-between;
				align-items: center;
				border: $border;
				padding: 10px;
				border-radius: $size1;
				margin-bottom: $size2;

				span {
					cursor: pointer;
				}

				.done {
					text-decoration: line-through;
				}

				button {
					cursor: pointer;
					font-size: $size2;
					background-color: rgb(236, 35, 243);
					border: 1px solid $primaryColor;
					font-weight: bold;
					color: white;
					padding: 5px 15px;
					border-radius: $size1;
				}
			}
		}

		h4 {
			text-align: center;
			opacity: 0.5;
			margin: 0;
		}
	}
}
</style>